package com.sysware.p2m.patch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

public class PatchPublishContext extends SystemPropertiesContext{

	// 发布补丁到本地的目录路径
	private String publishDir;
	// 是否发布源码文件（.java）
	private boolean isPublishJava = false;
	//小版本，从什么版本
	private String minVersion;
	
	// 临时补丁标记
	private boolean isTempPatch = false;
	//在SVN中的项目名称
	private String projectNameInSVN;
	
	//svn地址
	private String svnUrl;
	//svn用户名
	private String svnUserName;
	//svn密码
	private String svnPwd;
	// 黑名单目录，打补丁时排除在外的目录或文件
	protected String[] excludeConfigFile = {
			"oracle.properties",
			"fileserver_config.xml",
			"applicationUrl.xml",
			"caches.properties",
			"web.xml",
			//by dujg 过滤模型相关xml文件------------------------
			//如果版本已经在正式环境中使用，需要排除以下文件
			//打补丁的时候不会打出这些目录
			"data_model_fields.xml",
			"model_fields.xml",
			"model_relation_tether.xml",
			"model_operation.xml",
			"relation_views.xml",
			"data_template_model_fields.xml",
			"task_template_model_fields.xml"
			//------------
			};
	// 发布补丁时去除P2M集成配置项目录
	protected String[] excludeConfigFileDir = {
			// 集成地址配置
			"/WEB-INF/classes/task/variable",
			// IDE目录
			"/template.files/IDEFile",
			"/publishConfig",
			//by dujg 过滤模型相关目录------------------------
			//如果版本已经在正式环境中使用，需要排除以下文件目录
			//打补丁的时候不会打出这些目录
			"/WEB-INF/classes/task/datamodel",
			"/WEB-INF/classes/task/taskmodel",
			"/WEB-INF/classes/dm/datamodel",
			"/WEB-INF/classes/dm/taskmodel",
			"/WEB-INF/classes/com/sysware/p2m/task/model",
			"/WEB-INF/classes/com/sysware/p2m/data/model",
			"/WEB-INF/classes/com/sysware/dm/task/model",
			"/WEB-INF/classes/com/sysware/dm/data/model",
			"/WEB-INF/classes/task/taskfields",
			"/WEB-INF/classes/task/datafields"
			//--------------
			};
	// 白名单，打补丁时自动加入的文件，需要开发区相对路径，不支持目录（安全问题）
	protected String[] includeConfigFile = {
			"configuration/src/resources/properties/system.properties"
			};

	
	/********** 不需要设置的参数-START **********/
	// 补丁器要覆盖的目标文件名（现场部署的项目名）
	protected String deployName = "sysware";
	// 从SVN版本库获取日志信息缓存到本地的路径
	public final static String SVN_LOG_NAME = "svnlog.txt";
	public String svnLogFilePath;
	// 系统分标识隔符
	public final static String WEB_ROOT_STR = "WebRoot";
	public final static String WEBINF_CLASSES_STR = WEB_ROOT_STR + "/WEB-INF/classes";
	// 补丁目录，由PUBLISH_DIR+“/补丁名称”组成
	protected String patchDir;
	// 补丁中排除的配置文件集合
	protected Set<String> exculdeConfigFileSet = new HashSet<String>();
	// 补丁中排除的配置文件目录集合
	protected Set<String> exculdeConfigFileDirSet = new HashSet<String>();
	// 临时补丁标识
	protected final static String TEMP_PATCH_SIGN = "[#]";
	
	// 用于解析svnlog的关键字
	public static String SCHEMA_REVISION= "Revision: ";
	public static String SCHEMA_MODIFIED = "Modified : ";
	public static String SCHEMA_ADDED = "Added : ";
	public static String SCHEMA_DELETED = "Deleted : ";
	public Logger log = LoggerFactory.getLogger(getClass());
	/********** 不需要设置的参数-END **********/
	
	
	public String getPublishDir() {
		return publishDir;
	}
	public void setPublishDir(String publishDir) {
		this.publishDir = publishDir;
		this.svnLogFilePath = publishDir + "/" + SVN_LOG_NAME;
		this.patchDir = publishDir;
	}
	public boolean getIsPublishJava() {
		return isPublishJava;
	}
	public void setPublishJava(boolean isPublishJava) {
		this.isPublishJava = isPublishJava;
	}
	public String getMinVersion() {
		return minVersion;
	}
	public void setMinVersion(String minVersion) {
		this.minVersion = minVersion;
	}
	
	public boolean getTempPatch() {
		return isTempPatch;
	}
	public void setTempPatch(boolean isTempPatch) {
		this.isTempPatch = isTempPatch;
	}
	public String getPatchDir() {
		return patchDir;
	}
	public void setPatchDir(String patchDir) {
		this.patchDir = patchDir;
	}
	public String getDeployName() {
		return deployName;
	}
	public void setDeployName(String deployName) {
		this.deployName = deployName;
	}
	public void setProjectNameInSVN(String projectNameInSVN){
		this.projectNameInSVN=projectNameInSVN;
	}
	public String getProjectNameInSVN() {
		//如果配置则使用明确配置的名称
		if(this.projectNameInSVN!=null&&!this.projectNameInSVN.equals("")){
			return this.projectNameInSVN;
		}
//		// SVN版本库中项目名称，用于路径转换标识使用
		String svnProjectName = "syswareFramework";
		String svnUrl = getSvnUrl();
		if(svnUrl==null){
			//如果没有配置svn地址，则为默认的项目地址
			log.info("SVN url is null! use default projectNameInSVN ="+svnProjectName);
		}else{
			//如果配置了svn地址，则解析svn地址的项目地址
			String[] splits = svnUrl.split("/");
			if(splits.length<2){
				log.error("SVN url is error! svnUrl ="+svnUrl);
				return null;
			}
			if(splits[splits.length-1].trim().equals("")){
				svnProjectName=splits[splits.length-2];
			}else{
				svnProjectName=splits[splits.length-1];
			}
		}
		return svnProjectName;
	}
	public String getSvnUrl() {
		return svnUrl;
	}
	public void setSvnUrl(String svnUrl) {
		this.svnUrl = svnUrl;
	}
	public String getSvnUserName() {
		return svnUserName;
	}
	public void setSvnUserName(String svnUserName) {
		this.svnUserName = svnUserName;
	}
	public String getSvnPwd() {
		return svnPwd;
	}
	public void setSvnPwd(String svnPwd) {
		this.svnPwd = svnPwd;
	}
	
	
	public String[] getExcludeConfigFile() {
		return excludeConfigFile;
	}
	public void setExcludeConfigFile(String[] excludeConfigFile) {
		this.excludeConfigFile = excludeConfigFile;
	}
	public String[] getExcludeConfigFileDir() {
		return excludeConfigFileDir;
	}
	public void setExcludeConfigFileDir(String[] excludeConfigFileDir) {
		this.excludeConfigFileDir = excludeConfigFileDir;
	}
	public String[] getIncludeConfigFile() {
		return includeConfigFile;
	}
	public void setIncludeConfigFile(String[] includeConfigFile) {
		this.includeConfigFile = includeConfigFile;
	}
	
}
